package com.example.aboutspringboot.controller;

import com.example.aboutspringboot.entity.Img;
import com.example.aboutspringboot.entity.Shop;
import com.example.aboutspringboot.entity.ShopCar;
import com.example.aboutspringboot.service.ShopCarService;
import com.example.aboutspringboot.service.ShopService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("shop")
@CrossOrigin//跨域
public class ShopController {

    @Resource
    private ShopService shopService;

    @Resource
    private ShopCarService shopCarService;

    //查询所有商品信息
    @GetMapping("/selectShopAll")
    public Map<String, Object> selectShopAll() {
        Map<String, Object> map = new LinkedHashMap<>();
        List<Shop> list = shopService.selectShopAll();
        for (Shop shop : list) {
            shop.setImgSrc(shopService.selectImg(shop.getId()));
        }
        map.put("shop", list);
        return map;
    }

    //按id查找商品
    @GetMapping("/selectShopById")
    public Map<String, Object> selectShopById(long id,String username) {
        Map<String, Object> map = new LinkedHashMap<>();
        //加载购物车相关信息
        ShopCar shopCar= shopCarService.selectShopCarByIdAndName(username, id);
        if (shopCar==null){  //购物车中没有此商品
            map.put("shopNum",0);
        }else {
            map.put("shopNum",shopCar.getNumber());
        }
        Shop shop = shopService.selectShopById(id);

        shop.setImgSrc(shopService.selectImg(id));
        map.put("shop", shop);
        return map;
    }
    //查找所有商品图片信息
    @GetMapping("/selectImg")
    public Map<String,Object> selectImgAll(){
        Map<String, Object> map = new LinkedHashMap<>();
        List<Img> list=shopService.selectImgAll();
        map.put("img",list);
        return map;
    }
    //修改图片地址
    @PostMapping("/updateImg")
    public Map<String,Object> updateImg(@RequestBody Img img){
        Map<String, Object> map = new LinkedHashMap<>();
        int number=shopService.updateImgSrc(img);
        if(number>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }
        return map;
    }
    //删除图片
    @GetMapping("/deleteImg")
    public Map<String,Object> deleteImg(long imgId){
        Map<String, Object> map = new LinkedHashMap<>();
        int number=shopService.deleteImgSrc(imgId);
        if(number>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }
        return map;
    }
    //添加图片
    @PostMapping("/addImg")
    public Map<String,Object> addImg(@RequestBody Img img){
        Map<String, Object> map = new LinkedHashMap<>();
        int number=shopService.insertImgSrc(img);
        if(number>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }
        return map;
    }
    //添加商品文字信息
    @PostMapping("/addShop")
    public Map<String,Object> addShop(@RequestBody Shop shop){

        Map<String, Object> map = new LinkedHashMap<>();
        int number=shopService.insertShop(shop); //添加商品信息（不包括图片）
        if(number>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }
        return map;
    }
    //修改商品信息
    @PostMapping("/editShop")
    public Map<String,Object> editShop(@RequestBody Shop shop){
        Map<String, Object> map = new LinkedHashMap<>();
//        System.out.println("当前商品信息："+shop);
        int number=shopService.updateShop(shop);
        if(number>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }
        return map;
    }
}
